﻿using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core.Entities;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Glorysoft.EAP.Core
{
    public abstract class FreeSqlContext
    {
        protected readonly XfwContext Context = XfwContext.Current;
        protected static Logger FreeSqlLogger = XfwContext.LogManager.Create("FreeSqlLogger");
        public static string Name => "FreeSqlContext";
        //    protected virtual IFreeSql FreeSqlConnect()
        //    {
        //        var freeSqlConnect =
        // new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=" + Dict.Current.EAPDB + "")
        //.UseAutoSyncStructure(true) //自动同步实体结构到数据库
        //.Build();

        //        return freeSqlConnect;
        //    }

        public FreeSqlContext()
        {

        }
        public static IFreeSql FreeSqlConnect { get; } =
        new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=" + Dict.Current.EAPDB + "")
        .UseAutoSyncStructure(true) //自动同步实体结构到数据库
        .Build();

        public static int Insert<TEntity>(TEntity entity) where TEntity : class, IKey, new()
        {
            int result = 0;
            try
            {

                var query = FreeSqlConnect.Insert(entity);
               // FreeSqlLogger.Debug($"{Name}.Insert, QueryString: {query.ToSql()}");
                result = query.ExecuteAffrows();

            }
            catch (Exception e)
            {
                FreeSqlLogger.Error($"{Name}.Insert <{typeof(TEntity).Name}> error!", e);
                result = -1;
            }
            return result;
        }

        public static int Update<TEntity>(TEntity entity) where TEntity : class, IKey, new()
        {
            int result = 0;
            try
            {
                var query = FreeSqlConnect.Update<TEntity>().SetSource(entity); //FreeSqlConnect.Update<TEntity>(entity).SetSource(entity).Where(it => it.ID == entity.ID);
              //  FreeSqlLogger.Debug($"{Name}.Update, QueryString: {query.ToSql()}");
                result = query.ExecuteAffrows();
            }
            catch (Exception e)
            {
                FreeSqlLogger.Error($"{Name}.Update <{typeof(TEntity).Name}> error!", e);
                result = -1;
            }
            return result;
        }
        public static int Delete<TEntity>(int id) where TEntity : class, IKey, new()
        {
            int result = 0;
            try
            {

                var query = FreeSqlConnect.Delete<TEntity>().Where(item => item.ID == id);
             //   FreeSqlLogger.Debug($"{Name}.Delete, QueryString: {query.ToSql()}");
                result = query.ExecuteAffrows();

            }
            catch (Exception e)
            {
                FreeSqlLogger.Error($"{Name}.Delete <{typeof(TEntity).Name}> error!", e);
                result = -1;
            }
            return result;
        }

        public static IList<TEntity> SelectAll<TEntity>() where TEntity : class, IKey, new()
        {
            IList<TEntity> result = null;
            try
            {

                var query = FreeSqlConnect.Select<TEntity>();
               // FreeSqlLogger.Debug($"{Name}.SelectAll , QueryString: {query.ToSql()}");
                result = query.ToList();

            }
            catch (Exception e)
            {
                FreeSqlLogger.Error($"{Name}.SelectAll <{typeof(TEntity).Name}> error!", e);
                result = null;
            }
            return result;
        }
        public static TEntity SelectById<TEntity>(int id) where TEntity : class, IKey, new()
        {
            TEntity result = null;
            try
            {

                var query = FreeSqlConnect.Select<TEntity>().Where(item => item.ID == id);
              //  FreeSqlLogger.Debug($"{Name}.SelectById, QueryString: {query.ToSql()}");
                result = query.First();

            }
            catch (Exception e)
            {
                FreeSqlLogger.Error($"{Name}.SelectById <{typeof(TEntity).Name}> error!", e);
                result = null;
            }
            return result;
        }

    }


}
